package com.leaderment.timatt.webmagic.downloader;

import java.io.Closeable;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.leaderment.timatt.webmagic.extern.MyProxyProvider;
import com.leaderment.timatt.webmagic.extern.ProxyVO;
import com.leaderment.timatt.webmagic.extern.SeleniumAction;
import com.leaderment.timatt.webmagic.extern.UpsWebDriverPool;
import com.leaderment.timatt.webmagic.util.DataStorageToLocal;
import com.leaderment.timatt.webmagic.util.IDataStorage;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.PlainText;

/** 
* 1.重写SeleniumDownloader增加SeleniumAction接口用于扩展使用selenium API
* 2.WebDriverPool改MyWebDriverPool（增加设置代理）
* 3.驱动目前有chrome和phantomjs，需要使用其他驱动请自行下载
* @author HankJone
* @date 2019
*/
@SuppressWarnings("deprecation")
public class UpsSeleniumDownloader implements Downloader, Closeable {

	private volatile UpsWebDriverPool webDriverPool;

	private Logger logger = Logger.getLogger(getClass());

	private int sleepTime = 0;

	private int poolSize = 1;

	private SeleniumAction action=null;
	
	private MyProxyProvider proxyProvider;

	/**
	 * 新建
	 *
	 */
	public UpsSeleniumDownloader(SeleniumAction action){
		String separator=System.getProperty("file.separator");
		System.setProperty("selenuim_config", System.getProperty("user.dir")+separator+"webDriver"+separator+"config.ini");   
        this.action=action;
    }

	/**
	 * Constructor without any filed. Construct PhantomJS browser
	 * 
	 * @author bob.li.0718@gmail.com
	 */
	public UpsSeleniumDownloader() {
		// System.setProperty("phantomjs.binary.path",
		// "/Users/Bingo/Downloads/phantomjs-1.9.7-macosx/bin/phantomjs");
	}
	public UpsSeleniumDownloader(int sleepTime,UpsWebDriverPool pool){
        this(sleepTime,pool,null);
    }
    public UpsSeleniumDownloader(int sleepTime,UpsWebDriverPool pool,SeleniumAction action){
        this.sleepTime=sleepTime;
        this.action=action;
        if(pool!=null){
            webDriverPool=pool;
        }
    }
    
    //设置使用代理提供类
    public void setProxyProvider(MyProxyProvider proxyProvider) {
        this.proxyProvider = proxyProvider;
    }

	/**
	 * set sleep time to wait until load success
	 *
	 * @param sleepTime sleepTime
	 * @return this
	 */
	public UpsSeleniumDownloader setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
		return this;
	}

	@Override
	public Page download(Request request, Task task) {
		checkInit();
		WebDriver webDriver = null;
		try {
			//获取代理
			ProxyVO proxy = proxyProvider != null ? proxyProvider.getProxy(task) : null;
			webDriver = webDriverPool.get(proxy);
			logger.info("---------------------------------------begin downloading page " + request.getUrl());
			webDriver.manage().timeouts().pageLoadTimeout(77, TimeUnit.SECONDS);
			webDriver.get(request.getUrl());
			WebDriver.Options manage = webDriver.manage();
			Site site = task.getSite();
			if (site.getCookies() != null) {
				for (Map.Entry<String, String> cookieEntry : site.getCookies()
						.entrySet()) {
					Cookie cookie = new Cookie(cookieEntry.getKey(),
							cookieEntry.getValue());
					manage.addCookie(cookie);
				}
			}
			
			manage.window().maximize();	//浏览器最大化
			Thread.sleep(30000);
	        if(action!=null){
	        	action.execute(webDriver);
	        }
			
//			if(null != content && null != fileName) {
//				IDataStorage dataStorage = new DataStorageToLocal();
//				dataStorage.save(new Date(), fileName+"_"+200, request.getUrl(), 1, content);
//			}
			
			
			Page page = new Page();
			webDriver.close();
			return page;
		} catch (InterruptedException e) {
			logger.warn("interrupted", e);
			return null;
		}catch(Exception e) {
			logger.warn("interrupted", e);
			Page ePage = new Page();
			ePage.setUrl(new PlainText(request.getUrl()));
			return ePage;
		}finally {
			if(null != webDriver)webDriver.quit();
		}
		
	}

	private void checkInit() {
		if (webDriverPool == null) {
			synchronized (this) {
				webDriverPool = new UpsWebDriverPool(poolSize);
			}
		}
	}

	@Override
	public void setThread(int thread) {
		this.poolSize = thread;
	}

	@Override
	public void close() throws IOException {
		webDriverPool.closeAll();
	}
}
